package org.ala.tcp_feign_client.handler.close;

import org.ala.tcp_feign_client.context.TcpFeignClientContext;
import org.ala.tcp_feign_common.enums.TcpFeignMessageTypeEnums;
import org.ala.tcp_feign_common.handler.IByteMessageAction;
import org.ala.tcp_feign_common.logs.LoggerFactory;
import org.ala.tcp_feign_common.msg.close.CloseByteMessage;
import org.slf4j.Logger;

import io.netty.channel.ChannelHandlerContext;


/**
 * 断连消息
 *
 * @author 骆毅(ala)
 * @date 2024年4月20日
 */
public class CloseByteMessageAction implements IByteMessageAction<CloseByteMessage> {

	
	static Logger log = LoggerFactory.getLogger("tcp-feign", "client");
	
	
	@Override
	public TcpFeignMessageTypeEnums type() {
		return TcpFeignMessageTypeEnums.CLOSE;
	}

	@Override
	public void action(ChannelHandlerContext ctx, CloseByteMessage msg) {
		log.info("[CloseByteMessageAction action] 收到断连消息，即将断开连接. id:" + ctx.channel().id());
		
		String host = TcpFeignClientContext.hostFromChannel(ctx.channel());
		Integer port = TcpFeignClientContext.portFromChannel(ctx.channel());
		String suffix = TcpFeignClientContext.suffixFromChannel(ctx.channel());
		String username = TcpFeignClientContext.userFromChannel(ctx.channel());
		String password = TcpFeignClientContext.passwordFromChannel(ctx.channel());
		TcpFeignClientContext.instance().close(host, port, suffix, username, password);
	}

}
